package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.dao.core.UserToAgentDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.CouponUserDto;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.UserToAgent;
import com.tbit.uqbike.client.service.CouponUserService;
import com.tbit.uqbike.client.service.GeoService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域优惠券
 *
 * @Author: chen
 * @Description: 区域优惠券
 * @Date: 创建于 15:28 2020/1/3
 */
@Slf4j
@Controller
@RequestMapping("/couponUser")
public class CouponUserController {
    @Autowired
    private GeoService geoService;
    @Autowired
    private CouponUserService couponUserService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserToAgentDao userToAgentDao;

    /**
     * 通过经纬度获取区域优惠券
     *
     * @param token     令牌
     * @param accountId 帐户id
     * @param lo        经度
     * @param la        纬度
     * @param mapType   地图类型
     * @param response  响应
     * @ignoreParams request
     */
    @RequestMapping("/getByLocation")
    public void getByLocation(String token, Integer accountId, Double lo, Double la, Integer mapType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(accountId, "-10001", "品牌id不能为空");
            Assert.notNull(lo, "-2001", "经度不能为空");
            Assert.notNull(la, "-2002", "纬度不能为空");

            if (mapType == null) {
                mapType = MapConstant.MAP_GOOGLE;
            }

            Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);
            User user = tokenService.getUser(token);
            List<CouponUserDto> couponUserDto = new ArrayList<>();
            if (agentId == null) {
                UserToAgent userToAgent = userToAgentDao.getByUserId(user.getUserId());
                if (userToAgent != null) {
                    agentId = userToAgent.getAccountId();
                }
            }
            log.info("[获取区域优惠券] 查询区域id: {}，用户id: {}", agentId, user.getUserId());
            if (agentId != null) {
                /**获取区域优惠券*/
                couponUserDto = couponUserService.getByUserId(agentId, user.getUserId());
            }

            map.put(ResponseCode.DATA, couponUserDto);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}
